/*
 * producer_test.c
 *
 *  Created on: Jun 26, 2009
 *      Author: root
 */

#include "producer_test.h"
#include "producer.h"
#include "producer_consumer.h"
#include "log.h"
#include "common.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

void * producer_test(void * inData)
{
   request_list_container_t * list_container = initContainer(LIST_SIZE);

   request_t * req = NULL;
   int length;
   char * buffer = (char *)malloc(sizeof(char) * MAX_INT_CHAR_LENGTH);
   char * strData;
   unsigned int index;

   if (buffer == NULL)
   {
      writeError(SOURCE_PRODUCE_TEST, MALLOC_PRODUCED_NULL_BUFFER);
      return NULL;
   }

   for (index = 0; index < TEST_AMOUNT; ++index)
   {
      req = (request_t *)malloc(sizeof(request_t));
      length = sprintf(buffer, "%d", index);
      strData = (char *)malloc(sizeof(char) * (length + 1));
      // ensure strData is not NULL
      if (strData != NULL)
      {
         memcpy(strData, buffer, length + 1);
         produce(list_container, strData, req);
         writeLog(SOURCE_PRODUCE_TEST, req);
      }
      else
      {
         writeError(SOURCE_PRODUCE_STRING, MALLOC_PRODUCED_NULL_BUFFER);
      }
   }

   free(buffer);
   buffer = NULL;

   sem_post(&exitSem);

   pthread_exit(NULL);

   return NULL;
}
